(function($) {

// Si l'eventManager a été déclaré on le surcharge pour la gestion de event
if(typeof $.eventManager != 'undefined'){
	var em = $.eventManager;
	if(em.type == 'event'){		
		/*em.heightResize = function(eventBase){
			
		};*/
		
		em.eventEngine = function(eventBase, baseOptions) {
			var options = {};
			
			// Vérification des paramètres
			//console.log(em.isScriptRegistred('event'));
			if(typeof baseOptions.options.event == 'undefined'){
				eventBase.errorMessage(baseOptions.messages.errors.eventEngine.parameters); 
				return null;
			} else 
				options = baseOptions.options.event;
			
			return new Event(eventBase.content, options);
		};
	}
}

function Event(element, options){
	var t = this;
	
	// Variables locals
	var _element = element;
	var views;
	
	var isInit = false;
	
	var defaults = {
		data: {},
		templates: {
			base: '<div class="em-event-container" />',
			//html: '<div id="testvue"></div>',				// Paramètre à ajouter pour rajouter du contenu html au conteneur
			views: '<div class="em-views-container" />'
		}, 
		views: {}, 
		plugins: [],
		isNew: true
	};
	
	// exports
	t.options = $.extend(true, {}, defaults, options);	
	t.data = t.options.data;
	t.plugins = t.options.plugins;
	t.isNew = t.options.isNew;
	t.header;
	t.menu;
	t.content;
	t.contentHtml;
	
	t.render = render;
	t.viewsElement;

	init();
	
	/**
	 * Méthodes obligatoires appelé par le manager !
	 */
	function render() {
		if (!isInit) {
			initialRender();
		}

		if (t.header) 
			t.header.render();
		
		if(t.menu)
			t.menu.render();
		
		if(views || typeof t.options.templates.html != 'undefined'){			
			t.viewsElement = $(t.options.templates.views).appendTo(t.content);
			
			// S'il existe un template html on le charge après le container de vues
			if(typeof t.options.templates.html != 'undefined')
				t.contentHtml = $(t.options.templates.html).appendTo(t.content);
			
			$.each(views, function(name, dataView){
				dataView['name'] = name;
				var view = new EventView(t, dataView);
				
				view.render();
			});
		}
	}
	
	/**
	 * Méthodes complémentaires
	 */
	function init(){
		t.content = $(options.templates.base);
		
		// initialisation du header
		t.header = new EventHeader(t);
		
		// initialisation des tabs
		t.menu = new EventMenu(t);
		
		// Récupération des paramètrages des vues
		views = $.extend(true, {}, t.options.views, t.menu.views);
		
		// Récupération du paramétrage des plugins pour chaque vue
		$.each(views, function(nameView, optionsView){
			if(typeof optionsView.plugins != 'undefined')
				t.plugins = t.plugins.concat(optionsView.plugins);
		});
	}
	
	function initialRender(){
		isInit = true;
		t.content.prependTo(_element);
	}
}

function EventMenu(event) {
	var t = this;
		
	// variables locals
	var element;
	var content;
	var itemsElement;
	var defaults = {
		templates:{
			base: '<ul id="em-menu-container" />',
			item: '<li class="em-menu" />'
		},
		menuItems: {},
		element: event.content
	};
	var defaultsItem = {
		template: '<a class="em-menu-link" href="#" />',
		onClick: function(event){}
	};

	var options = $.extend(true, {},
		defaults,
		event.options.menu
	);
	
	element = options.element;
	t.views = {};
	t.links = new Array();
	
	// export des méthodes
	t.render = render;
	
	init();
	
	function init() {
		$.each(options.menuItems, function(id, data){
			data = $.extend(true, {},
				defaultsItem,
				this
			);
			
			t.links.push(data);
			
			// générations des vues en fonction du menu
			if(typeof data.view != 'undefined'){
				data.view['id'] = id; 
				
				var viewJson = {};
				viewJson[id] = data.view;
				t.views = $.extend(true, {}, t.views, viewJson);
			}
		});
	}
	
	function render() {
		if (!content) {
			initialRender();
		}
		
		$.each(t.links, function(id, data){			
			var item = $(options.templates.item).appendTo(content);

			var link = $(data.template).appendTo(item);
			
			if(typeof data.link != 'undefined' && typeof data.title != 'undefined'){
				link.attr('id', 'em-menu-'+id);
				link.attr('href', data.link);
				link.attr('title', data.title);
				link.find('.em-title').html(data.title);
			}
			
			// Attachement de l'event click surchargé
			link.click(function(e){ data.onClick(e); });
		});
		
		$(content).parents('aside').affix({
			offset: {
				top: 0
			}
		});
	}
	
	function initialRender(){
		if(t.links.length > 0)
			content = $(options.templates.base).appendTo(element);
	}
}

function EventView(event, options) {
	var t = this;
	
	// variables locals
	var element = event.viewsElement;
	var content;
	var defaults = {
		templates:{
			base: '<div class="em-view" />'
			//html: '<div id="testvue"></div>',				// Paramètre à ajouter pour rajouter du contenu html au conteneur
		},
		content: 'La vue est vide !',
		plugins:[]
	};

	options = $.extend(true, {},
		defaults,
		options
	);
	
	// export des méthodes
	t.render = render;
	
	if(typeof options.idElement != 'undefined'){
		element = $(options.idElement);
		
		var newView = {};
		if(typeof options.name != 'undefined')
			newView[options.name] = options;
		else
			newView[options.idElement.replace(/[^a-zA-Z0-9_]/,'')] = options;
		event.viewsElement = $.extend(true, {}, event.viewsElement, options);
	}
	
	function render() {
		content = $(options.templates.base).appendTo(element);
		
		content.attr('id', options.name);
		content.append(options.content);
		
		// S'il existe un template html on le charge après la vue
		if(typeof options.templates.html != 'undefined')
			content.append($(t.options.templates.html));
	}
}

function EventHeader(event) {
	var t = this;

	// variables locals
	var element = event.content;
	var content;
	
	var title;
	var subTitle;
	var notifBar;	
	
	var stringDate;
	var stringDateFin;
	
	// exports
	t.render = render;
	t.setTitle = setTitle;	
	t.setStringDate = setStringDate;
	t.setStringDateFin = setStringDateFin;

	function render() {
		if(!stringDate){
			var date = new Date(event.data.date.date.replace(/\s/, 'T'));
	
			stringDate = date.getDate() < 10 ? '0'+date.getDate()+'/' : date.getDate()+'/';
			stringDate += date.getMonth() < 10 ? '0'+date.getMonth()+'/' : date.getMonth()+'/';
			stringDate += date.getFullYear();
		}
		
		if(!stringDateFin){
			var dateFin = new Date(event.data.dateFin.date.replace(/\s/, 'T'));
	
			stringDateFin = dateFin.getDate() < 10 ? '0'+dateFin.getDate()+'/' : dateFin.getDate()+'/';
			stringDateFin += dateFin.getMonth() < 10 ? '0'+dateFin.getMonth()+'/' : dateFin.getMonth()+'/';
			stringDateFin += dateFin.getFullYear();
		}
		
		if(!content)
			content = $('<div class="page-header" id="main-header" />').appendTo(element);
		else
			$(content).html('');
		
		notifBar = $('<div class="breadcrumb" />')
			.appendTo(content)
			.html('barre de notification ...');
		
		title = $('<h1 id="main-heading" />')
			.appendTo(content)
			.html(event.data.title);
		
		subTitle = $('<span />')
			.appendTo(title)
			.html('Du '+stringDate+' au '+stringDateFin);
	}
	
	function setTitle(newTitle){
		title = newTitle;
	}
	
	function setStringDate(date){
		stringDate = date;
	}
	
	function setStringDateFin(date){
		stringDateFin = date;
	}
}

})(jQuery);